package com.example.coffeeseller_2.widget;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.example.coffeeseller_2.R;

/**
 * Created by WH on 2018/7/24.
 * 自定义购买流程view
 */

public class BuyProcessView extends View {

    private static String TAG = "BuyProcessView";
    public static int CHOOSE_COFFEE = 0;
    public static int TASTE_PAY = 1;
    public static int MAKING = 2;
    public static int TAKE_CUP = 3;

    private static int PROCESS_WAIT = 0;
    private static int PROCESS_DOING = 1;
    private static int PROCESS_HAS_DONE = 2;
    private int noteNum = 4;//节点数量
//    private int width, height;
    private int avgLenght;//每部分平均长度
    private int cicleRadius;
    private int textTopPadding;
    private int centerY;
    private int textSize;
    private int textColor, textChooseColor;
    private Paint linePaint;
    private Paint textPaint;
    private Paint bitmapPaint;
    private int curState;
    private String[] texts;
    private Bitmap hasFinishBitmap;
    private Bitmap doingBitmap;
    private Bitmap notHasFinishBitmap;
    private int bitmapWidth, bitmapHeight;

    public BuyProcessView(Context context) {
        super(context);
    }

    public BuyProcessView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public BuyProcessView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BuyProcessView);
        int strokeWidth,lineColor;
        try{
            curState = typedArray.getInt(R.styleable.BuyProcessView_state, 0);
            cicleRadius = typedArray.getInt(R.styleable.BuyProcessView_cicleRadius, 10);
            textTopPadding = (int) typedArray.getDimension(R.styleable.BuyProcessView_textTopPadding, 10) * 4 / 3;
            textSize = (int) typedArray.getDimension(R.styleable.BuyProcessView_textSize, 20);
            bitmapWidth = (int) typedArray.getDimension(R.styleable.BuyProcessView_bitmapWidth, 20) * 4 / 3;
            bitmapHeight = (int) typedArray.getDimension(R.styleable.BuyProcessView_bitmapHeight, 20) * 4 / 3;
            strokeWidth = (int) typedArray.getDimension(R.styleable.BuyProcessView_strokeWidth, 2);
            textColor = typedArray.getColor(R.styleable.BuyProcessView_textColor, ContextCompat.getColor(context, R.color.white));
            lineColor = typedArray.getColor(R.styleable.BuyProcessView_lineColor, ContextCompat.getColor(context, R.color.white));
            textChooseColor = typedArray.getColor(R.styleable.BuyProcessView_textChooseColor, ContextCompat.getColor(context, R.color.white));

        }finally {
            typedArray.recycle();
        }

        texts = new String[]{
                context.getString(R.string.text_choose_coffee), context.getString(R.string.text_taste_pay)
                , context.getString(R.string.text_making), context.getString(R.string.text_takecup)
        };

        linePaint = new Paint();
        linePaint.setAntiAlias(true);
        linePaint.setColor(lineColor);
        linePaint.setStrokeWidth(strokeWidth);

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);
        textPaint.setAlpha(150);

        bitmapPaint = new Paint();
        bitmapPaint.setAntiAlias(true);
        bitmapPaint.setColor(lineColor);
        bitmapPaint.setStrokeWidth(strokeWidth);

        Resources resources = context.getResources();
        hasFinishBitmap = BitmapFactory.decodeResource(resources, R.mipmap.process_has_done);
        notHasFinishBitmap = BitmapFactory.decodeResource(resources, R.mipmap.process_wait);
        doingBitmap = BitmapFactory.decodeResource(resources, R.mipmap.process_doing);

        hasFinishBitmap = setBitmap(hasFinishBitmap);
        notHasFinishBitmap = setBitmap(notHasFinishBitmap);
        doingBitmap = setBitmap(doingBitmap);
    }


    public void setState(int state) {

        curState = state;

        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = widthMeasureSpec / 4 * 3;
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthMeasureSpec;

        }

        int height = doingBitmap.getHeight() + textTopPadding + 3 * textSize;
        this.setMeasuredDimension(width, height);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        avgLenght = (width - 50) / noteNum;
        centerY = height / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (int i = 0; i < noteNum; i++) {
            int centerX = (i + 1) * avgLenght;
            int nextCenterX = (i + 2) * avgLenght;

            drawByState(canvas, i, centerX, nextCenterX);
        }
    }

    private void drawByState(Canvas canvas, int index, int centerX, int nextCenterX) {
        switch (curState) {
            case 0:
                if (index == curState) {

                    drawBitmap(PROCESS_DOING, centerX, canvas, bitmapPaint);
                } else {

                    drawBitmap(PROCESS_WAIT, centerX, canvas, bitmapPaint);
                }
                drawLine(canvas, centerX, index, nextCenterX);
                drawText(textColor, canvas, texts[index], centerX);
                break;
            default:
                if (index < curState) {

                    drawBitmap(PROCESS_HAS_DONE, centerX, canvas, bitmapPaint);
                    drawText(textColor, canvas, texts[index], centerX);
                } else if (index == curState) {

                    drawBitmap(PROCESS_DOING, centerX, canvas, bitmapPaint);
                    drawText(textChooseColor, canvas, texts[index], centerX);
                } else {

                    drawBitmap(PROCESS_WAIT, centerX, canvas, bitmapPaint);
                    drawText(textColor, canvas, texts[index], centerX);
                }
                drawLine(canvas, centerX, index, nextCenterX);
                break;
        }
    }

    private void drawText(int color, Canvas canvas, String text1, int centerX) {
        String text = text1;
        Rect rect = new Rect();
        textPaint.setColor(color);
        textPaint.getTextBounds(text, 0, text.length(), rect);
        canvas.drawText(text, centerX - rect.width() / 2, centerY + cicleRadius + textTopPadding, textPaint);
    }

    private void drawLine(Canvas canvas, int centerX, int index, int nextCenterX) {
        if (index + 1 == noteNum) {
            return;
        }
        canvas.drawLine(centerX + notHasFinishBitmap.getWidth() / 2, centerY, nextCenterX - notHasFinishBitmap.getWidth() / 2, centerY, linePaint);

    }

    /*
   * isFill:是否是实心
   * isAlpha:是否透明
   * */
    private void drawCicles(boolean isFill, int centerX, Canvas canvas, Paint paint) {

        if (isFill) {
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            int[] colors = new int[]{// 渐变色数组 7色
                    0xFF00FF00, 0xFFFFFF00,
                    0xFFFF0000};
            LinearGradient shader = new LinearGradient(centerX - cicleRadius, centerY - cicleRadius, // 渐变区域,左上右下
                    centerX + cicleRadius, centerY + cicleRadius, colors, null, Shader.TileMode.REPEAT);
            paint.setShader(shader);
        } else {
            paint.setStyle(Paint.Style.STROKE);
            paint.setShader(null);
        }
        canvas.drawCircle(centerX, centerY, cicleRadius, paint);
    }

    private void drawBitmap(int bitmapState, int centerX, Canvas canvas, Paint paint) {

        if (bitmapState == PROCESS_WAIT) { //如果未执行

            canvas.drawBitmap(notHasFinishBitmap, centerX - notHasFinishBitmap.getWidth() / 2, centerY - notHasFinishBitmap.getHeight() / 2, paint);
        } else if (bitmapState == PROCESS_DOING) {

            canvas.drawBitmap(doingBitmap, centerX - doingBitmap.getWidth() / 2, centerY - doingBitmap.getHeight() / 2, paint);

        } else if (bitmapState == PROCESS_HAS_DONE) {

            canvas.drawBitmap(hasFinishBitmap, centerX - hasFinishBitmap.getWidth() / 2, centerY - hasFinishBitmap.getHeight() / 2, paint);
        }
    }

    private Bitmap setBitmap(Bitmap bitmap) {
        Bitmap target;
        if (bitmapWidth <= bitmap.getWidth() || bitmapHeight <= bitmap.getHeight()) {
            target = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        } else {
            target = Bitmap.createBitmap(bitmapWidth, bitmapHeight, bitmap.getConfig());

        }

        Canvas temp_canvas = new Canvas(target);
        temp_canvas.drawBitmap(bitmap, null, new Rect(0, 0, target.getWidth(), target.getHeight()), null);
        return target;
    }
}
